package com.litxuf.logic.equipment.manager;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import com.litxuf.FinalInformation;
import com.litxuf.logic.core.ManagerFactory;
import com.litxuf.logic.equipment.dao.EquipmentDAO;
import com.litxuf.logic.equipment.dao.model.Equipment;
import com.litxuf.logic.equipment.dao.model.EquipmentProcessStep;
import com.litxuf.logic.equipment.dao.model.EquipmentStepParameter;

public class EquipmentManager {
	Logger logger = Logger.getLogger(this.getClass());

	private EquipmentDAO equipmentDAO;

	public EquipmentDAO getEquipmentDAO() {
		return equipmentDAO;
	}

	public void setEquipmentDAO(EquipmentDAO equipmentDAO) {
		this.equipmentDAO = equipmentDAO;
	}

	/**
	 * 查询设备类型 made in 2011-3-26下午11:54:30
	 * 
	 * @param equipmentType
	 * @param jiguan
	 * @param buqing
	 * @return
	 * 
	 */
	public Equipment findEquipment(String equipmentType, String jiguan, String buqing) {
		return equipmentDAO.findEquipment(equipmentType, jiguan, buqing);
	}

	public Set processJieGuan(int jiguangTotal, Set params, int h) {

		Set param_set = new HashSet();

		Iterator it = params.iterator();
		while (it.hasNext()) {
			EquipmentStepParameter param = (EquipmentStepParameter) it.next();

			EquipmentStepParameter param_new = new EquipmentStepParameter();

			try {
				BeanUtils.copyProperties(param_new, param);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			param_new.setParameterNameEn(param.getParameterNameEn() + "_" + h);

			param_set.add(param_new);
		}

		return param_set;
	}

	public Map findParam2webpage(int jiguangTotal, Set steps) {

		Map m = new HashMap();
		Iterator its = steps.iterator();
		while (its.hasNext()) {

			WebParama web = new WebParama();
			EquipmentProcessStep step = (EquipmentProcessStep)its.next();
			web.setIsJieguan(step.getIsJieguan());
			web.setProcessStepImgurl(step.getProcessStepImgurl());
			web.setProcessStepName(step.getProcessStepName());
			web.setProcessStepType(step.getProcessStepType());
			web.setProcessStepId(step.getProcessStepId());

			List paramsList = new ArrayList();
			web.setParams(paramsList);
			logger.info("--->"+step.getProcessStepName());
			// 一般参数 接管处理
			if (jiguangTotal > 0
					&& step.getIsJieguan().equals("Y")
					&& step.getProcessStepType().equals(
							FinalInformation.process_step_type_YIBAN)) {

				for (int h = 1; h < jiguangTotal+1; h++) {
					Set params = step.getEquipmentStepParametes();
					paramsList.add(processJieGuan(jiguangTotal, params, h));
				}

				m.put("step_" + step.getProcessStepOrder(), web);
			}

			/**
			 * 一般参数 非接管
			 */
			if (step.getProcessStepType().equals(
					FinalInformation.process_step_type_YIBAN)
					&& !step.getIsJieguan().equals("Y")) {
				paramsList.add(step.getEquipmentStepParametes());
				m.put("step_" + step.getProcessStepOrder(), web);
			}

			/**
			 * 材料
			 */

			if (step.getProcessStepType().equals(
					FinalInformation.process_step_type_CAILIAO)) {

				paramsList.add(step.getEquipmentStepParametes());
				m.put("cailiao_" + step.getProcessStepOrder(), web);

			}

			/**
			 * 载荷 接管
			 */

			if (step.getProcessStepType().equals(
					FinalInformation.process_step_type_ZAIHE)
					&& jiguangTotal > 0 && step.getIsJieguan().equals("Y")) {

				for (int h = 1; h < jiguangTotal+1; h++) {
					Set params = step.getEquipmentStepParametes();
					paramsList.add(processJieGuan(jiguangTotal, params, h));
				}
				
				m.put("zaihe_" + step.getProcessStepOrder(), web);

			}

			/**
			 * 载荷 非接管
			 */
			if (step.getProcessStepType().equals(
					FinalInformation.process_step_type_ZAIHE)
					&& !step.getIsJieguan().equals("Y")) {

				paramsList.add(step.getEquipmentStepParametes());
				m.put("zaihe_" + step.getProcessStepOrder(), web);
			}

		}

		return m;
	}

	public static void main(String[] args) {
		EquipmentManager eq = ManagerFactory.getFactory().getEquipmentManager();

		Equipment e = eq.findEquipment("QGFT", "R", "A");
		
	Map m=	eq.findParam2webpage(3, e.getEquipmentProcessSteps());
		System.out.println(m);
	}
}
